package com.xiaoc.bulldozer.common.util;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

public abstract class ReportTemplate<T> {

    private String name;
    private HSSFWorkbook workbook;
    private HttpServletResponse response;

    public HSSFWorkbook getWorkbook() {
        return workbook;
    }

    public void setWorkbook(HSSFWorkbook workbook) {
        this.workbook = workbook;
    }

    public String getName() {
        return name;
    }

    public HttpServletResponse getResponse() {
        return response;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setResponse(HttpServletResponse response) {
        this.response = response;
    }

    /**
     * 生成一张报表，一个sheet的方法
     * @throws IOException
     */
    public void createReport() throws IOException {
        //工作簿
        this.workbook = new HSSFWorkbook();
        setParam();
        setData();
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-disposition", "attachment;filename="+new String(name.getBytes("utf-8"),"ISO-8859-1" ));
        OutputStream outputStream = response.getOutputStream();
        workbook.write(outputStream);
        outputStream.flush();
        outputStream.close();
    }

    /**
     * 获取报表需要的数据
     */
    public abstract void setData();

    /**
     * 配置报表对象需要的参数
     */
    public abstract void setParam();
}
